BINPATH="$(dirname "$0")"
. "$BINPATH/bili.cfg"
curl="/usr/bin/curl -s -K curl.config -b \"$BINPATH/$cookie\" -c \"$BINPATH/$cookie\""

if eval $curl 'http://bilibili.us/member/ajax_loginsta.php' | iconv -fgbk | grep -qs 你好！ ; then
    echo 'already logged in' >&2
else
    #not logged in
    echo 'not logged in' >&2
    #$curl http://bilibili.us/member/ajax_loginsta.php -o /dev/null
    eval $curl http://bilibili.us/login.php -o /dev/null
    tmp="$(tempfile -s.jpg)"
    eval $curl 'http://bilibili.us/include/vdimgck.php -o "$tmp"'
    display "$tmp" & sleep 1
    rm "$tmp"
    echo -n code: >&2
    read code
    eval $curl 'http://bilibili.us/member/index_do.php -d "fmdo=login&dopost=login&keeptime=604800&userid=$user&pwd=$pass&vdcode=$code" -o /dev/null'
    result="$(eval $curl http://bilibili.us/member/ajax_loginsta.php | iconv -fgbk | grep -o 你好！)"
    echo "$result"
    if [ -z "$result" ]; then
	echo 'login failed' >&2
	exit 1
    fi
fi
